<!-- Here is the page source code for 06pieCreate.php -->

<?php
  session_start();    // Parameters will be passed using session variables.
                      // Parameters, in this case, are the numbers that control the size of the slices of pie.

  // EXTRACT DATA POSTED TO THIS PAGE IN THE SESSION VARIABLE ARRAY
  $n1 = $_SESSION['n1'];
  $n2 = $_SESSION['n2'];
  $n3 = $_SESSION['n3']; 
  $n4 = $_SESSION['n4']; 
  $n5 = $_SESSION['n5']; 

  // ================================================================================================
  // This function draws two lines at the correct angle and an arc. This makes a cake/pie slice.
  // This outline is flood filled.
  // ================================================================================================
  function arc($image, $centreX, $centreY, $radius, $startAngle, $endAngle, $colour)
  {
    imagefilledrectangle($image, $centreX - 3, $centreY - 3, $centreX + 3, $centreY + 3, $colour);

    $angle =  2 * pi() * $startAngle / 360;    // degrees converted to radians
    imageline($image, $centreX, $centreY, $centreX + $radius * cos($angle), $centreY + $radius * sin($angle), $colour);
    imagefilledrectangle($image, $centreX + $radius * cos($angle) - 3, $centreY + $radius * sin($angle) - 3, 
                                 $centreX + $radius * cos($angle) + 3, $centreY + $radius * sin($angle) + 3, $colour);
	
    $angle =  2 * pi() * $endAngle / 360;    // degrees converted to radians
    imageline($image, $centreX, $centreY, $centreX + $radius * cos($angle), $centreY + $radius * sin($angle), $colour);
    imagefilledrectangle($image, $centreX + $radius * cos($angle) - 3, $centreY + $radius * sin($angle) - 3, 
                                 $centreX + $radius * cos($angle) + 3, $centreY + $radius * sin($angle) + 3, $colour);
	
    imagearc($image, $centreX, $centreY, 2 * $radius, 2 * $radius, $startAngle, $endAngle, $colour);
	  
    $fillAngle =  2 * pi() * (($startAngle + $endAngle) / 2) / 360;    // degrees converted to radians
    imagefill($image, $centreX + (2 * $radius / 3) * cos($fillAngle), $centreY + (2 * $radius / 3) * sin($fillAngle), $colour);
  }
  // ================================================================================================

  header( "Content-type: image/png");                       // Tells the browser to expect a png image
  
  // CREATE THE IMAGE (500 across by 600 down)
  $image = imagecreate(500, 600);                           // Create a new 500 x 600 image
  
  // SET UP SOME COLOURS
  $yellow  = imagecolorallocate($image, 255, 255,   0);      // create color R=255, G=255, B=0
  $cyan    = imagecolorallocate($image,   0, 255, 255);      // create color R=0, G=255, B=255
  $red     = imagecolorallocate($image, 255,   0,   0);      // create color red
  $blue    = imagecolorallocate($image,   0,   0, 255);      // create color blue
  $lime    = imagecolorallocate($image,   0, 255,   0);      // create color lime
  $magenta = imagecolorallocate($image, 255,   0, 255);      // create color magenta
  
  // DRAW SOME SHAPES
  imagefilledrectangle($image,  0,  0, 500, 600, $cyan);    // create cyan background
  imagefilledrectangle($image, 10, 80, 490, 590, $yellow);  // create inner yellow rectangle 
  imagerectangle      ($image,  5,  5, 495, 595, $red);     // create red frame
  
  // DISPLAY SOME TEXT
  imagestring($image, 5, 15, 1 * 20, "The input numbers were : $n1  $n2  $n3  $n4  $n5", $red);
  imagestring($image, 5, 15, 2 * 20, "Sometimes flood fill messes up in this program.", $red);
  imagestring($image, 5, 15, 3 * 20, "Fix it by using imagefilledarc() with GD >= 2.0.1", $red);
  
  // Add up input data to find total amount.
  $total = $n1 + $n2 + $n3 + $n4 + $n5;
  
  if (($n1 < 0) or ($n2 < 0) or ($n3 < 0) or ($n4 < 0) or ($n5 < 0))
  {
    // Error message for invalid negative input data
    imagestring($image, 5, 15, 10 + 4 * 20, "Negative inputs are not allowed.", $red);
  }
  else if ($total == 0)
  {
    // Error message for invalid input data
    imagestring($image, 5, 15, 10 + 4 * 20, "Input data missing or totalled zero.", $red);
  }
  else
  {
    $startAngle = 0;                                           // 3 o'clock on the pie chart
    $endAngle   = $StartAngle + 360 * $n1 / $total;            // The fraction $n1 is of a complete circle
    arc($image, 250, 350, 200, $startAngle, $endAngle, $red);  // centreX, centreY, radius, startAngle, endAngle, colour
	
    $startAngle = $endAngle;
    $endAngle   = $endAngle + 360 * $n2 / $total;  
    arc($image, 250, 350, 200, $startAngle, $endAngle, $blue); // centreX, centreY, radius, startAngle, endAngle, colour
	    
    $startAngle = $endAngle;
    $endAngle   = $endAngle + 360 * $n3 / $total;  
    arc($image, 250, 350, 200, $startAngle, $endAngle, $lime); // centreX, centreY, radius, startAngle, endAngle, colour
    
    $startAngle = $endAngle;
    $endAngle   = $endAngle + 360 * $n4 / $total;  
    arc($image, 250, 350, 200, $startAngle, $endAngle, $cyan); // centreX, centreY, radius, startAngle, endAngle, colour
	  
    $startAngle = $endAngle;
    $endAngle   = $endAngle + 360 * $n5 / $total;  
    arc($image, 250, 350, 200, $startAngle, $endAngle, $magenta); // centreX, centreY, radius, startAngle, endAngle, colour
  }

  imagepng($image);                                               // Save image to file.

  imagedestroy($image);                                           // Cleanup memory  
?>